package com.app.asktom.model;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.app.asktom.impl.ImplUser;

public class User extends SQLiteOpenHelper implements ImplUser{
	
	private final String TABLE_CREATE = "CREATE TABLE IF NOT " +
		"EXISTS tbl_user (id integer primary key " +
		"autoincrement, phoneNumber text not null, " +
		"password text not null, activationCode text not null," +
		" isActivated text not null, name text not null, " +
		"isSendFriend not null)";
	private int userid;
	private String phone = "";
	private String activationCode;
	private String isActivated;
	private String name;
	private String password;
	private String isSendFriend;
	public static String sPhoneNumber;
	
	public String getIsSendFriend() {
		return isSendFriend;
	}

	public void setIsSendFriend(String isSendFriend) {
		this.isSendFriend = isSendFriend;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private static final String DATABASE_NAME = "asktom.db";
	private static final int DATABASE_VERSION = 1;	
	
	public User(Context context, String name, CursorFactory factory, 
			int version) {
		super(context, name, factory, version);
	}	

	public String getActivationCode() {
		return activationCode;
	}

	public void setActivationCode(String activationCode) {
		this.activationCode = activationCode;
	}

	public String getIsActivated() {
		return isActivated;
	}

	public void setIsActivated(String isActivated) {
		this.isActivated = isActivated;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public User(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}	
	
	public int getUserid() {
		return userid;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "User [userid=" + userid + ", phone=" + phone + "]";
	}

	@Override
	public String getUserNumber() {
		return this.getPhone();
	}

	@Override
	public boolean insertUser() {
		boolean isSucces = false;
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("phoneNumber", this.phone);
		values.put("name", this.name);
		values.put("activationCode", this.activationCode);
		values.put("isActivated", this.isActivated);
		values.put("password", this.password);
		values.put("isSendFriend",this.isSendFriend);
		
		if(db.insert("tbl_user", null, values) >= 0) {
			isSucces = true;
		}
		db.close();
		
		return isSucces;
	}

	public boolean updateUserSetActivated(String activationCode) {
		boolean isSuccess = false;
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("isActivated", "TRUE");
		db.update("tbl_user", values, "activationCode=?", new String[]{activationCode});
		return isSuccess;
	}
	
	public boolean updateUserSetSendFriendNumber() {
		boolean isSuccess = false;
		
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("isSendFriend", "TRUE");
		db.update("tbl_user", values, "isActivated=?", new String[]{"TRUE"});
		db.close();
		return isSuccess;
	}

	public Map<String, String> getUserInfoByPhone(
			String phoneNumber) {
		Map<String,String> map = new HashMap<String, String>();
		String sql = "SELECT id, phoneNumber, name, " +
				"password, activationCode, isActivated FROM tbl_user";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.getCount() > 0) {
			if(cursor.moveToFirst()) {
				map.put("id", cursor.getString(0));
				map.put("phoneNumber", cursor.getString(1));
				map.put("name",cursor.getString(2));
				map.put("password",cursor.getString(3));
				map.put("activationCode",cursor.getString(4));
				map.put("isActivated", cursor.getString(5));
			}			
		}


		cursor.close();
		db.close();
		return map;
	}
	
	public boolean isUserActivated() {
		boolean isSucces = false;
		String sql = "SELECT phoneNumber FROM tbl_user " +
				"WHERE isActivated = 'TRUE'";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.getCount() > 0) {
			isSucces = true;
		}
		cursor.close();
		db.close();
		return isSucces;
	}
	
	public boolean isUserRegistered() {
		boolean isSucces = false;
		String sql = "SELECT phoneNumber FROM tbl_user ";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.getCount() > 0) {
			isSucces = true;
		}
		cursor.close();
		db.close();
		return isSucces;
	}
	
	public boolean isSendFriendsNumberAlreadySend() {
		boolean isSucces = false;
		String sql = "SELECT phoneNumber FROM tbl_user " +
				"WHERE isSendFriend = 'TRUE'";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.getCount() > 0) {
			isSucces = true;
		}
		cursor.close();
		db.close();		
		return isSucces;
	}
	
	public String getUserPhoneNumber() {
		
		String sql = "SELECT phoneNumber FROM tbl_user " +
				"WHERE isActivated = 'TRUE'";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.getCount() > 0) {
			if(cursor.moveToFirst()) {
				this.phone = cursor.getString(0);
			}
		}
		cursor.close();
		db.close();
		return this.phone;
	}
	
	public boolean isUploader(String number){
		boolean uploader = false;
		String sql = "SELECT phoneNumber FROM tbl_user WHERE phoneNumber=?";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, new String[]{number});
		if(cursor.getCount()>0){
			uploader = true;
		}
		
		return uploader;
	}
	
	public Map<String,String> getUserInfo(String activationCode) {
		Map<String,String> map = new HashMap<String, String>();
		String sql = "SELECT id, phoneNumber, name, " +
				"password, activationCode, isActivated " +
				"FROM tbl_user WHERE activationCode = '" +
				activationCode + "'";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.getCount() > 0) {
			if(cursor.moveToFirst()) {
				map.put("id", cursor.getString(0));
				map.put("phoneNumber", cursor.getString(1));
				map.put("password",cursor.getString(2));
				map.put("activationCode",cursor.getString(3));
				map.put("isActivated", cursor.getString(4));
				map.put("name",cursor.getString(5));
			}			
		}

		cursor.close();
		db.close();		
		return map;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion,
			int newVersion) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Map<String, String> getUserInfo() {
		Map<String,String> map = new HashMap<String, String>();
		String sql = "SELECT * FROM tbl_user WHERE " +
				"isActivated = 'TRUE'";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if(cursor.moveToFirst()) {
			map.put("id", cursor.getString(0));
			map.put("phoneNumber", cursor.getString(1));
			map.put("password",cursor.getString(2));
			map.put("activationCode",cursor.getString(3));
			map.put("isActivated", cursor.getString(4));			
			map.put("name",cursor.getString(5));
		}
		db.close();
		return map;
	}

	@Override
	public boolean updateUser() {
		// TODO Auto-generated method stub
		return false;
	}
}
